package com.shop.thread;

import org.springframework.stereotype.Component;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

@Component
public class AsyncConfigurer implements org.springframework.scheduling.annotation.AsyncConfigurer {

    @Override
    public Executor getAsyncExecutor() {
        VisiableThreadPoolTaskExecutor threadPool = new VisiableThreadPoolTaskExecutor();
        threadPool.setCorePoolSize(500);//当前线程数
        threadPool.setMaxPoolSize(2000);// 最大线程数
        threadPool.setQueueCapacity(10000);//线程池所使用的缓冲队列
        threadPool.setWaitForTasksToCompleteOnShutdown(true);//等待任务在关机时完成--表明等待所有线程执行完
        threadPool.setAwaitTerminationSeconds(5);// 等待时间 （默认为0，此时立即停止），并没等待xx秒后强制停止
        threadPool.setThreadNamePrefix("rental-Async-");//  线程名称前缀
        threadPool.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        threadPool.initialize(); // 初始化
        return threadPool;
    }
}
